package com.example.testcomposeproject.pages.sideEffect.testRememberUpdateState

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.testcomposeproject.PreViewData
import kotlinx.coroutines.delay

/**
 * @author: hemin
 * @date: 2024/6/21
 * @Desc:
 *
 */
@Preview(showBackground = true, device = PreViewData.PreviewPhoneSize)
@Composable
fun PreViewTestRememberUpdateStateUI(modifier: Modifier = Modifier) {
        TestRememberUpdateStateUI()
}


/**
 * 真正的UI元素
 */
@Composable
fun TestRememberUpdateStateUI() {

    val timeOut1: () -> Unit = {
        println("timeOut1")
    }

    val timeOut2: () -> Unit = {
        println("timeOut2")
    }

    var currentTimeout by remember {
        mutableStateOf(timeOut1)
    }

    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = androidx.compose.ui.Alignment.CenterHorizontally,
        verticalArrangement = androidx.compose.foundation.layout.Arrangement.Center
    ) {
        Button(onClick = {
            if (currentTimeout == timeOut1) {
                currentTimeout = timeOut2
            } else {
                currentTimeout = timeOut1
            }
        }

        ) {
            Text(text = if (currentTimeout  == timeOut1) {"timeOut1"} else {"timeOut2"})
        }

        LandingScreen { currentTimeout()}
    }

}


@Composable
fun LandingScreen(onTimeout: () -> Unit) {
    LaunchedEffect(Unit) {
        println("开始倒计时")
        repeat(10) {

            delay(1000)
            println("倒计时:${it+1}")
        }
        onTimeout()
    }

}




